﻿<?php

class Bizprofile_Widgets_Owner_Widget extends Tomato_Widget
{
    protected function _prepareShow()
    {
        $enterprise_id = 0;
        $user_id = 0;
        $auth = Zend_Auth::getInstance();
        $enterprise = null;
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();

        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
            if($user->enterprise){
                $enterprise_id = $user->enterprise->id;
                $user_id = $user->user_id;
                $enterprise = $user->enterprise;
            }
        }

        $params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
        $lang = Zend_Controller_Front::getInstance()->getRequest()->getParam('lang');
		
		$frontEndActive = null;
		
        if($params['module'] == 'bizprofile' && isset($params['enterprise_id'])){
            $enterprise_id = $params['enterprise_id'];
            $enterpriseDao  = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
            $enterpriseDao->setDbConnection($conn);
            $enterprise = $enterpriseDao->getById($enterprise_id);
			if($enterprise->frontend_active==1)
				$frontEndActive = true;
        }
		$this->_view->assign("frontEndActive",$frontEndActive);
        $user_entity = -1;
        if($enterprise_id > 0 && $enterprise){
            $this->_view->assign('enterprise', $enterprise);
            $permissionDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPermissionDao();
            $permissionDao->setDbConnection($conn);
            //$user_entity = $permissionDao->getUserEntity($user_id,$enterprise_id);
			/** Khanh
	        * Kiem tra la user chua login, hay user da login, la DN khach, hay DN doi tac
			* $user_entity = 0 : User da login, va dang xem chinh doanh nghiep cua minh
	        * $user_entity = 1 : User chua login
	        * $user_entity = 2 : User da login, nhung ko la DN
	        * $user_entity = 3 : User da login, la DN khach
	        * $user_entity = 4 : User da login, la DN doi tac			
	        **/
            if(isset($params['enterprise_id']))
			{
				//$auth = Zend_Auth::getInstance();
		        if ($auth->hasIdentity()) 
		        { 
		            $user = $auth->getIdentity();
		            if(!isset($user->enterprise->id)){
			        	$user_entity = 2; // user da login, nhung ko phai la DN
			        }
			        else{
						if($params['enterprise_id'] == $user->enterprise->id){
							$user_entity = 0; 
						}else{
							$partnerDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPartnerDao();
							$partnerDao->setDbConnection($conn);
			
							$pageIndex     = 1;
							$perPage = 5;
							$offset     = ($pageIndex - 1) * $perPage;
							$exp = array(
								'relationid' 		=> $params['enterprise_id'],
								'partner_type' 		=> 'buddy',
								'partner_status' 	=> 'yes',
								'deleted'		 	=> 0
							);							
							//* In ralationship
							$partners = $partnerDao->find($offset, $perPage, $exp);
							$numPartners   = $partnerDao->count($exp);
							
							$user_entity = 3;// DN khach
							$request = $this->getRequest();
							foreach($partners as $row):
								$expRecomment = array(
									'relationid'    => $row->relationid,
									'enterprise_id' => $row->enterprise_id, // DN doi tac
								);
								if($user->enterprise->id == $row->enterprise_id)
								{
									$user_entity = 4; // DN doi tac
								}
							endforeach;
						}
			        }
		        }
		        else 
		        {
		        	$user_entity = 1; // user chua login 
		        }
			}
			
			/**
			* END : Kiem tra la user chua login, hay user da login, la DN khach, hay DN doi tac
		    **/
            	
            $infoDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getInfoDao();
            $infoDao->setDbConnection($conn);
            $infoDao->setLang($lang);
            $infos = $infoDao->getInfos($enterprise_id, 5);
            $this->_view->assign('infos', $infos);
        }
        if($user_entity >= 3){ // DA LOGIN LA DOANH NGHIEP KHACH HOAC DOANH NGHIEP DOI TAC
            $partnerDao = Tomato_Model_Dao_Factory::getInstance()->setModule("bizprofile")->getPartnerDao();
            $partnerDao->setDbConnection($conn);
			// mối quan hệ giữa mình với enterprise này
            $partner = $partnerDao->getPartner_($user->enterprise->id, $enterprise->id);						
			$this->_view->assign('partnerMeYou',$partner);

			// mối quan hệ giữa enterprise này với mình
            $partner_to = $partnerDao->getPartner_($enterprise->id, $user->enterprise->id);
			$this->_view->assign('partnerYouMe',$partner_to);

            $this->_view->assign('enterprise_id', $user->enterprise->id);
            $this->_view->assign('relation_id', $enterprise->id);
        }
		$user_enterprise_id = isset($user->enterprise->id)?isset($user->enterprise->id):null;
		
		$permissionMessage = $permissionDao->getPermissionByUserEntity($user_enterprise_id,$user_entity);
			
		$this->_view->assign('permissionMessage',$permissionMessage);
		
        $purl = $params['module'] . "_" . strtolower($params['controller']) . "_" . str_replace("-","_",$params['action']);		
		$enterprise_id     =   isset($params['enterprise_id'])?$params['enterprise_id']:null;
		$enterprise_name   =   isset($params['enterprise_name'])?$params['enterprise_name']:null;
		if($enterprise_id){
			$arrPartnerRoute = array(
				'enterprise_id' => $enterprise_id,
				'enterprise_name' => Tomato_Utility_String::removeSign($enterprise_name, '-', true),
			);
			$this->_view->assign('arrPartnerRoute', $arrPartnerRoute);		
			$this->_view->assign('purl', $purl);
		}
        $this->_view->assign('user_entity', $user_entity);
    }

    protected function _prepareConfig()
    {

    }
}
